package com.zzs.team.Controller;

import com.google.gson.Gson;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.zzs.team.DAO.LikeDao;
import com.zzs.team.Entity.Location;
import com.zzs.team.Entity.Team;
import com.zzs.team.Entity.TeamShow;
import com.zzs.team.Service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.logging.SimpleFormatter;

@Controller
@ResponseBody
public class TeamController {

    @Autowired
    TeamService teamService;

    @PostMapping("/team/getTeamById")
    public Map getTeamById(String userid,Integer teamid){
        System.out.println(userid);
        Map<String,TeamShow> map = new HashMap<>();
        TeamShow teamShow = teamService.getTeamById(userid,teamid);
        map.put("team",teamShow);
        return map;
    }

    @PostMapping("/team/putTeams")
    public void putTeam(String team){
        Gson gson = new Gson();
        Team t = gson.fromJson(team,Team.class);
        teamService.insertTeam(t);
    }

    /**
     * 进入首页查询所有的队伍信息条目
     * @param userid 用来收集当前用户的点赞的队伍信息id 用于在首页判断是否点赞
     * @return
     */
    @PostMapping("/team/getTeams")
    public Map getTeams(String userid){
        Map map = new HashMap();
        List<TeamShow> teams = teamService.getTeams(userid);
        map.put("teams",teams);
        return map;
    }

    @GetMapping("/team/getTeamsWithoutUserID")
    public Map getTeamsWithoutUserID(){
        Map map = new HashMap();
        try {
            List<TeamShow> teamShows = teamService.getTeamsWithoutUserID();
            map.put("teams",teamShows);
            map.put("msg",1);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg",0);
        }
        return map;
    }

    /**
     * 查询当前用户发布的所有组队信息
     * @param userID
     * @return
     */
    @PostMapping("/team/getMyTeams")
    public Map getMyTeams(String userID){
        Map map = new HashMap();
        try{
            List<TeamShow> myteams = teamService.getMyTeams(userID);
            map.put("myteams",myteams);
            map.put("msg",1);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg",0);
        }
        return map;
    }

    /**
     * 用户删除自己发布的组队信息（不能只要信息id，否则别人只需要输入id就可以随意删除数据库）
     * @param userID 用户的id
     * @param teamID 组队信息的id
     * @return
     */
    @PostMapping("/team/deleteTeamByTeamid")
    public Map deleteTeamByTeamid(String userID,String teamID){
        Map map = new HashMap();
        try {
            teamService.deleteTeamByTeamid(userID, teamID);
            map.put("msg",1);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg",0);
        }
        return map;
    }

    /**
     * 用户编辑更改自己发布的组队信息
     * @param userID
     * @param team
     * @return
     */
    @PostMapping("/team/updateTeam")
    public Map updateTeam(String userID,String team){
        Map map = new HashMap();
        try{
            Gson gson = new Gson();
            Team t = gson.fromJson(team,Team.class);
            teamService.updateTeam(userID,t);
            map.put("msg",1);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg",0);
        }
        return map;
    }

    @GetMapping("/team/getTeamsByLocation")
    public Map getTeamsByLocation(String userID,String location){
        Map map = new HashMap();
        try{
            Gson gson = new Gson();
            Location loc = gson.fromJson(location,Location.class);

            List<TeamShow> teams= new ArrayList<>();


            if (loc.getProvinceIndex()==0){
                //1、没有选择省份，则应该查找出所有组队信息
                teams =  teamService.getTeams(userID);
            }else if (loc.getCityIndex()==0){
                //2、选择了省份但是没有选择城市,要判断选择的是直辖市还是其他的
                if ("北京".equals(loc.getProvinceName()) || "上海".equals(loc.getProvinceName()) || "天津".equals(loc.getProvinceName()) || "重庆".equals(loc.getProvinceName())){
                    //2.1选择了省份且是直辖市
                    //判断选择整个直辖市还是直辖市里的学校
                    if (loc.getSchoolIndex()==0){
                        //2.1.1 选择直辖市但没有选择学校，则查出直辖市的所有学校
                        teams = teamService.getTeamsByCityName(userID,loc.getCityName());
                    }else{
                        //2.1.2如果选择直辖市并且学则学校了，则查出该学校的信息
                        teams = teamService.getTeamsBySchoolName(userID,loc.getSchoolName());
                    }
                }else{
                    //2.2选择了省份但不是直辖市
                    teams = teamService.getTeamsByProvinceName(userID,loc.getProvinceName());
                }
            }else if (loc.getSchoolIndex()==0){
                //3、选择了省份、城市 但没有选择学校，则找出该城市的所有组队信息
                teams = teamService.getTeamsByCityName(userID, loc.getCityName());
            }else{
                //4、选择了省份、城市、学校，则找出该学校的所有组队信息
                teams = teamService.getTeamsBySchoolName(userID,loc.getSchoolName());
            }
            if (teams.size()>0){
                map.put("teams",teams);  //如果不为孔则放入数组
            }else {
                map.put("teams",0);  //为空放入0
            }
            map.put("msg",1);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg",0);
        }
        return  map;
    }

}
